package com.colorful.insuranceproject.claims;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.adapter.ClaimsProgressAdapter;
import com.colorful.insuranceproject.base.PLEFragment;
import com.colorful.insuranceproject.bean.AboutClaimsProgressBean;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.ClaimsRequest;
import com.lovely3x.common.activities.refresh.CanLoadMore;
import com.lovely3x.common.activities.refresh.CanRefresh;
import com.lovely3x.common.adapter.ListAdapter;
import com.lovely3x.common.utils.Response;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by handsome-Bob on 2017/11/7.
 * <p>
 * 进度列表
 */
public class ClaimsProgressFragment extends PLEFragment implements ListAdapter.ViewClickedListener, CanLoadMore, CanRefresh, View.OnClickListener {

    public static ClaimsProgressFragment newInstance() {
        Bundle bundle = new Bundle();
        ClaimsProgressFragment fragment = new ClaimsProgressFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    private static final int GET_CLAIMS_PROGRESS_URL = 0X1;

    private ClaimsRequest claimsRequest;

    private ClaimsProgressAdapter claimsProgressAdapter;

    /**
     * 列表展示数据
     */
    @Bind(R.id.lv_show_progress)
    ListView lvShowProgress;

    @Bind(R.id.fl_fragment_list_empty_container)
    ViewGroup vgEmptyTipContainer;

    @Override
    protected int getContentView() {
        return R.layout.activity_claims_progress;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this, mRootView);
//        setTitle(getString(R.string.claims_progress));
//        addRightBtn(getString(R.string.screen), R.id.screen);
//        findViewById(R.id.screen).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showToast(getString(R.string.screen));
//            }
//        });
    }

    @Override
    protected void onViewInitialized() {
        claimsProgressAdapter = new ClaimsProgressAdapter(null, mActivity);
        lvShowProgress.setAdapter(claimsProgressAdapter);
        claimsProgressAdapter.setOnViewClickedListener(this);
        claimsRequest = new ClaimsRequest(getHandler());

        handleLoadingContent();
        currentIndex = DEFAULT_POST_FIRST_PAGE;
        getPageData();
    }

    /**
     * 请求页面数据
     */
    private void getPageData() {
        if (claimsRequest != null) {
            claimsRequest.getClaimsProgress(currentIndex, GET_CLAIMS_PROGRESS_URL);
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            //  获取数据
            case GET_CLAIMS_PROGRESS_URL:
                handleRefreshState(response, claimsProgressAdapter);
                if (response.isSuccessful) {
                    final List<AboutClaimsProgressBean> listBean = (List<AboutClaimsProgressBean>) response.obj;
                    if (currentIndex == 0 && (listBean).size() == 0) {
                        onContentStatusChanged(LOADING_FAILURE, getString(R.string.not_find_data), this);
                    } else {
                        setPageData(listBean, response.addtional);
                        onContentStatusChanged(LOADING_SUCCESSFUL);
                    }
                } else {
                    if (response.addtional == DEFAULT_POST_FIRST_PAGE) {
                        onContentStatusChanged(LOADING_FAILURE, CodeTable.getInstance().getCodeDescription(response), this);
                    }
                }
                break;
        }
    }

    private void setPageData(List<AboutClaimsProgressBean> obj, int index) {
        if (claimsProgressAdapter != null) {
            switch (index) {
                case DEFAULT_POST_FIRST_PAGE:
                    claimsProgressAdapter.setData(obj);
                    break;
                default:
                    claimsProgressAdapter.addDataToLast(obj);
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        handleLoadingContent();
        currentIndex = DEFAULT_POST_FIRST_PAGE;
        getPageData();
    }

    @Override
    protected ViewGroup getEmptyContainerView() {
        return vgEmptyTipContainer;
    }

    @Override
    public void onBeginRefresh() {
        currentIndex = DEFAULT_POST_FIRST_PAGE;
        claimsRequest.getClaimsProgress(currentIndex, GET_CLAIMS_PROGRESS_URL);

    }

    @Override
    public void onBeginLoadMore() {
        currentIndex++;
        claimsRequest.getClaimsProgress(currentIndex, GET_CLAIMS_PROGRESS_URL);
    }

    @Override
    public void onViewClicked(int position, View view) {
        AboutClaimsProgressBean bean = claimsProgressAdapter.getItem(position);
        mActivity.launchActivity(ClaimsDetailsActivity.class, ClaimsDetailsActivity.EXTRA_CLAIMS_ORDER_ID, bean.getInsuranceId());
    }
}
